package com.fyzk.activity.user.coupon;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.lottery.basketball.JCLQFragmentActivityI;
import com.fyzk.activity.lottery.gaopin.AnHuiK3Activity;
import com.fyzk.activity.lottery.gaopin.GD11x5Activity;
import com.fyzk.activity.lottery.gaopin.GDKLSFActivity;
import com.fyzk.activity.lottery.gaopin.GXK3Activity;
import com.fyzk.activity.lottery.gaopin.JLK3Activity;
import com.fyzk.activity.lottery.gaopin.JSK3Activity;
import com.fyzk.activity.lottery.gaopin.KLSFActivity;
import com.fyzk.activity.lottery.gaopin.NXK3Activity;
import com.fyzk.activity.lottery.gaopin.SD11x5Activity;
import com.fyzk.activity.lottery.gaopin.SSCActivity;
import com.fyzk.activity.lottery.gaopin.XJ11x5Activity;
import com.fyzk.activity.lottery.jc.football.newJz.FootballFragmentActivityI;
import com.fyzk.activity.lottery.shuzicai.DLTActivity;
import com.fyzk.activity.lottery.shuzicai.FC3DActivity;
import com.fyzk.activity.lottery.shuzicai.PL3Activity;
import com.fyzk.activity.lottery.shuzicai.PL5Activity;
import com.fyzk.activity.lottery.shuzicai.QLCActivity;
import com.fyzk.activity.lottery.shuzicai.QXCActivity;
import com.fyzk.activity.lottery.shuzicai.SSQActivity;
import com.fyzk.activity.lottery.shuzicai.klpk.KLPKActivity;
import com.fyzk.activity.lottery.zc_new.OldFootballBQCActivity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballJQCActivity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballR9Activity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballSFCActivity_New;
import com.fyzk.bean.CouponListBean;
import com.fyzk.bean.LotteryLayoutItem;
import com.fyzk.data.AppLotteryInfos;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.L;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.LotteryItemView;
import com.fyzk.view.TopTitleView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by fanxs11 on 17/1/23.
 *
 * 优惠劵详情
 */
public class CouponDetail extends BaseActivity {

    private ArrayList<LotteryItemView> lotteryItemViews = new ArrayList<LotteryItemView>();

    private CouponListBean.CouponsBean info;
    private TopTitleView topView;


    private LinearLayout ll;
    private TextView name_tv;
    private TextView time_tv;
    private TextView name2_tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.coupon_detail);

        info = (CouponListBean.CouponsBean) getIntent().getSerializableExtra("coupon");

        requestLotteryTypeLayout();

        initView();

        GridView gv = (GridView) findViewById(R.id.coupon_detail_gv);
        gv.setAdapter(lotteryAdapter);
        gv.setFocusable(false);
    }

    private void initView(){
        topView = (TopTitleView) findViewById(R.id.coupon_detail_topview);
        topView.setCenterText(R.string.coupon_detail_title);
        topView.setRightVisible(View.GONE);
        topView.setOnTitleClickListener(this);

        ll = (LinearLayout) findViewById(R.id.coupon_detail_ll);
        name_tv = (TextView) findViewById(R.id.coupon_detail_name);
        time_tv = (TextView) findViewById(R.id.coupon_detail_time);
        name2_tv = (TextView) findViewById(R.id.coupon_detail_name2);

        if (info.getStatus().equals("1")) {
            ll.setBackgroundResource(R.drawable.coupon_item_nouse_bg);
            name_tv.setTextColor(getResources().getColor(R.color.color_fa5e38));
        } else if (info.getStatus().equals("2")) {
            ll.setBackgroundResource(R.drawable.coupon_item_use_bg);
            name_tv.setTextColor(getResources().getColor(R.color.color_b4b1aa));
        } else if (info.getStatus().equals("3")) {
            ll.setBackgroundResource(R.drawable.coupon_item_overdue_bg);
            name_tv.setTextColor(getResources().getColor(R.color.color_b4b1aa));

        }

        name_tv.setText(info.getTitle());
        if (info.getEndDate().length() > 10) {
            time_tv.setText(info.getEndDate().substring(0, 10));  //去掉 00:00:00,只显示 2016-04-25
        }else {
            time_tv.setText(info.getEndDate());
        }
        name2_tv.setText("满"+info.getOrderamt()+"减"+info.getDisamt());

    }

    private void requestLotteryTypeLayout() {
        JSONObject json = new JSONObject();
        httpPostJson(CommandUtil.LOTTERYTYPELAYOUT, Commands.LOTTERYTYPELAYOUT, json, true);
    }

    List<LotteryLayoutItem> lotteryLayoutItemList;

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.LOTTERYTYPELAYOUT:
                lotteryLayoutItemList = JSON.parseArray(resultBean.getData(), LotteryLayoutItem
                        .class);
                initLotteryItemViewList();
                checkItem(info.getLotteryTypes());
                lotteryAdapter.notifyDataSetChanged();
                break;
            default:
                break;
        }
    }

    private void initLotteryItemViewList() {
        for (LotteryLayoutItem info : lotteryLayoutItemList) {
            LotteryItemView itemVIew = new LotteryItemView(this,
                    AppLotteryInfos.getLogoId(info.getLotteryType()));
            info.setLogoRes(AppLotteryInfos.getLogoId(info.getLotteryType()));
            itemVIew.setId(info.getLotteryType());// 以lotteryId 为新View 的id值
            itemVIew.setLotteryInfo(info);
            itemVIew.setOnClickListener(this);
            if (AppLotteryInfos.getcurrent_version_lotterys().contains(info
                    .getLotteryType())) {
                lotteryItemViews.add(itemVIew);
            }
        }


    }

    /**
     * 筛选符合本优惠劵的彩种
     * @param lotteryType   "3010,4010","0"
     */
    private void checkItem(String lotteryType){
        if (lotteryType.equals("0")) {  // 通用优惠劵
            return;
        }
        for (int i = 0; i < lotteryItemViews.size(); i++) {
            int lotteryId = lotteryItemViews.get(i).getLotteryId();
            if (lotteryId == LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT) { // 彩种列表以3006代表竞彩足球
                if (!(lotteryType.contains(LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT+"") || lotteryType.contains(LotteryIDUtils.JZSPF_LOTTERY_ID_INT+"")
                        || lotteryType.contains(LotteryIDUtils.JZBF_LOTTERY_ID_INT+"")  || lotteryType.contains(LotteryIDUtils.JZBQC_LOTTERY_ID_INT+"")
                        || lotteryType.contains(LotteryIDUtils.JZZJQ_LOTTERY_ID_INT+"")  || lotteryType.contains(LotteryIDUtils.JZHH_LOTTERY_ID_INT+""))) {

                    lotteryItemViews.remove(i);
                    i--;

                }
            } else if (lotteryId == LotteryIDUtils.JLSF_LOTTERY_ID_INT) { // 彩种列表以3001代表竞彩篮球
                if (!(lotteryType.contains(LotteryIDUtils.JLSF_LOTTERY_ID_INT+"") || lotteryType.contains(LotteryIDUtils.JLRSF_LOTTERY_ID_INT+"")
                        || lotteryType.contains(LotteryIDUtils.JLSFC_LOTTERY_ID_INT+"")  || lotteryType.contains(LotteryIDUtils.JLDXF_LOTTERY_ID_INT+"")
                        || lotteryType.contains(LotteryIDUtils.JLHH_LOTTERY_ID_INT +""))) {

                    lotteryItemViews.remove(i);
                    i--;

                }
            } else {
                String id = "" + lotteryItemViews.get(i).getLotteryId();
                if (lotteryType.contains(id)) {

                } else {
                    lotteryItemViews.remove(i);
                    i--;
                }
            }

        }
    }

    BaseAdapter lotteryAdapter = new BaseAdapter() {

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            return lotteryItemViews.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return lotteryItemViews.size();
        }
    };

    @Override
    public void onClick(View arg0) {
        Intent intent = new Intent();
        switch (arg0.getId()) {
            case LotteryIDUtils.SSQ_LOTTERY_ID_INT:
                intent.setClass(getContext(), SSQActivity.class);
                break;
            case LotteryIDUtils.DLT_LOTTERY_ID_INT:
                intent.setClass(getContext(), DLTActivity.class);
                break;
            case LotteryIDUtils.QXC_LOTTERY_ID_INT:
                intent.setClass(getContext(), QXCActivity.class);
                break;
            case LotteryIDUtils.JZSPF_LOTTERY_ID_INT:
            case LotteryIDUtils.JZBF_LOTTERY_ID_INT:
            case LotteryIDUtils.JZBQC_LOTTERY_ID_INT:
            case LotteryIDUtils.JZZJQ_LOTTERY_ID_INT:
            case LotteryIDUtils.JZHH_LOTTERY_ID_INT:
            case LotteryIDUtils.JZRFSPF_LOTTERY_ID_INT:
                intent.setClass(getContext(), FootballFragmentActivityI.class);
                break;
            case LotteryIDUtils.JZDG_LOTTERY_ID_INT:
                // 竞猜足球单关
                intent.setClass(getContext(), FootballFragmentActivityI.class);
                intent.putExtra("isDan", true);
                break;
            case LotteryIDUtils.ZCSFC_LOTTERY_ID_INT:
                intent.setClass(getContext(), OldFootballSFCActivity_New.class);
                break;
            case LotteryIDUtils.ZCRXJ_LOTTERY_ID_INT:
                intent.setClass(getContext(), OldFootballR9Activity_New.class);
                break;
            case LotteryIDUtils.PL3_LOTTERY_ID_INT:
                intent.setClass(getContext(), PL3Activity.class);
                break;
            case LotteryIDUtils.PL5_LOTTERY_ID_INT:
                intent.setClass(getContext(), PL5Activity.class);
                break;
            case LotteryIDUtils.FC3D_LOTTERY_ID_INT:
                intent.setClass(getContext(), FC3DActivity.class);
                break;
            case LotteryIDUtils.ZCBQC_LOTTERY_ID_INT:
                intent.setClass(getContext(), OldFootballBQCActivity_New.class);
                break;
            case LotteryIDUtils.QLC_LOTTERY_ID_INT:
                intent.setClass(getContext(), QLCActivity.class);
                break;
            case LotteryIDUtils.ZCJQC_LOTTERY_ID_INT:
                intent.setClass(getContext(), OldFootballJQCActivity_New.class);
                break;
            case LotteryIDUtils.SD11XUAN5_LOTTERY_ID_INT:
                intent.setClass(getContext(), SD11x5Activity.class);
                break;
            case LotteryIDUtils.GD11XUAN5_LOTTERY_ID_INT:
                intent.setClass(getContext(), GD11x5Activity.class);
                break;
            case LotteryIDUtils.XJ11XUAN5_LOTTERY_ID_INT:
                intent.setClass(getContext(), XJ11x5Activity.class);
                break;
            case LotteryIDUtils.CQSSC_LOTTERY_ID_INT:
                intent.setClass(getContext(), SSCActivity.class);
                break;
            case LotteryIDUtils.NXK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), NXK3Activity.class);
                break;
            case LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), AnHuiK3Activity.class);
                break;
            case LotteryIDUtils.JSK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), JSK3Activity.class);
                break;
            case LotteryIDUtils.JLK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), JLK3Activity.class);
                break;
            case LotteryIDUtils.JLSF_LOTTERY_ID_INT:
                intent.setClass(getContext(), JCLQFragmentActivityI.class);
                break;
            case LotteryIDUtils.KLSF_LOTTERY_ID_INT:
                intent.setClass(getContext(), KLSFActivity.class);
                break;
            case LotteryIDUtils.GDKLSF_LOTTERY_ID_INT:
                intent.setClass(getContext(), GDKLSFActivity.class);
                break;
            case LotteryIDUtils.KLPK_LOTTERY_ID_INT:
                intent.setClass(getContext(), KLPKActivity.class);
                break;
            case LotteryIDUtils.GXK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), GXK3Activity.class);
                break;
        }
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            L.e(TAG, e.getMessage());
        }
    }
}
